﻿using System.Collections;
using YooAsset;

namespace LFrameworkAOT 
{
    public class FsmDownloadPackageFiles : IStateNode
    {
        private StateMachine m_Machine;
        PatchOperation m_PatchOperation;

        void IStateNode.OnCreate(StateMachine machine)
        {
            m_Machine = machine;
            m_PatchOperation = m_Machine.Owner as PatchOperation;
        }
        void IStateNode.OnEnter()
        {
            m_PatchOperation.StartDownloadPackageFiles();
            GameLaunch.Instance.StartCoroutine(BeginDownload());
        }
        void IStateNode.OnUpdate()
        {
        }
        void IStateNode.OnExit()
        {
        }

        private IEnumerator BeginDownload()
        {
            var downloader = (ResourceDownloaderOperation)m_Machine.GetBlackboardValue("Downloader");
            downloader.DownloadErrorCallback = m_PatchOperation.WebFileDownloadFailed;
            downloader.DownloadUpdateCallback = m_PatchOperation.DownloadUpdateing;
            downloader.BeginDownload();
            yield return downloader;

            // 检测下载结果
            if (downloader.Status != EOperationStatus.Succeed)
                yield break;
        }
    }
}
